package com.dna.service.impl;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dna.entity.RoleEntity;
import com.dna.mapper.RoleMapper;
import com.dna.service.RoleService;
import com.dna.utils.Constant.UserState;
import com.dna.utils.Query;

import tk.mybatis.mapper.entity.Example;

@Service
public class RoleServiceImpl implements RoleService {
    
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public RoleEntity queryOne(RoleEntity ro) {
        return roleMapper.selectOne(ro);
    }

    @Override
    @Transactional
    public void save(RoleEntity ro) {
        if (roleMapper.insertSelective(ro)>0) {
            roleMapper.saveRealtion(ro);
        }
       
        
    }

    @Override
    @Transactional
    public void update(RoleEntity ro) {
        if (roleMapper.updateByPrimaryKeySelective(ro)>0) {
            roleMapper.deleteRealtion(ro);
            roleMapper.saveRealtion(ro);
        }
        
        
    }

    @Override
    @Transactional
    public void delete(Set<Long> ids) {
       
       Example example =new Example(RoleEntity.class);
       example.createCriteria().andIn("id", ids);
       RoleEntity record=new RoleEntity();
       record.setState(UserState.DELETE.getValue());
       if (roleMapper.updateByExampleSelective(record, example)>0) {
        roleMapper.deleteRealtionByIds(ids);
    }
        
    }

    @Override
    public List<RoleEntity> queryAll() {
        RoleEntity record=new RoleEntity();
        record.setState(UserState.NORMAL.getValue());
        return roleMapper.select(record);
    }

    @Override
    public Long queryTotal(Query<RoleEntity> query) {
        RoleEntity role=query.getT();
        return roleMapper.queryTotal(role);
    }

    @Override
    public List<RoleEntity> queryPageList(Query<RoleEntity> query) {
        RoleEntity role=query.getT();
        Integer limit=query.getLimit();
        Integer startRow=query.getStartRow();
        return roleMapper.queryPageList(limit,startRow,role);
    }

	
}
